package com.florent37.runtime_permission;


import com.florent37.runtime_permission.slice.AskPermissionSlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.bundle.IBundleManager;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

public class AskPermission extends Ability {
    private Reference<Ability> activityReference;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(AskPermissionSlice.class.getName());
        this.activityReference = new WeakReference<Ability>(this);
        ArrayList list = intent.getStringArrayListParam("getPermission");
        getPermission(list);
    }

    public void getPermission(ArrayList list) {
        String[] strings = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            strings[i] = (String) list.get(i);
        }

        if (verifySelfPermission((String) list.get(0)) != IBundleManager.PERMISSION_GRANTED) {
            requestPermissionsFromUser(
                    strings, 123);
        }

    }

    public void showCommonDialog(Context context) {
        DirectionalLayout directionalLayout = (DirectionalLayout) LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_common_dialog, null, false);
        Button button = (Button) directionalLayout.findComponentById(ResourceTable.Id_yes);
        Button button2 = (Button) directionalLayout.findComponentById(ResourceTable.Id_no);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                final Ability fractionAbility = activityReference.get();
                if (fractionAbility != null) {
                    Intent intent = new Intent();
                    intent.setAction(IntentConstants.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
                    intent.setUri(Uri.getUriFromParts("package", fractionAbility.getBundleName(), null));
                    fractionAbility.startAbility(intent);
                }
            }
        });
        button2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
        new CommonDialog(context)
                .setContentCustomComponent(directionalLayout)
                .setAutoClosable(true)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setAlignment(LayoutAlignment.BOTTOM)
                .show();
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);

        ToastDialog toastDialog = new ToastDialog(this);
        ToastDialog toastDialog1 = new ToastDialog(this);
        for (int grant : grantResults) {
            if (grant != IBundleManager.PERMISSION_GRANTED) {
                showCommonDialog(getContext());
            } else {
                toastDialog.setText("您同意了权限！").setDuration(5000).show();
            }
        }
    }
}
